//
//  NewsHeaderView.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/8.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "NewsHeaderView.h"
#import "NewsUserView.h"
#import "NewsDetailView.h"
#import "NewsShareView.h"
#import "XMLabel.h"

@interface NewsHeaderView ()
/// 标题
@property(nonatomic, strong)UILabel *titleLabel;
/// 作者视图
@property(nonatomic, strong)NewsUserView *userView;
/// 新闻详情
@property(nonatomic, strong)NewsDetailView *detailView;
/// 新闻来源
@property(nonatomic, strong)UILabel *sourceLabel;
/// 新闻作者
@property(nonatomic, strong)UILabel *authorLabel;
@property(nonatomic, assign)CGFloat detailHeight;

/// 声明
@property(nonatomic, strong)XMLabel *statementLabel;

@property(nonatomic, strong)NewsShareView *shareView;
///评论的分割线
@property(nonatomic, strong)UIView *lineView;
@property(nonatomic, strong)UILabel *allCommentLabel;

@end

@implementation NewsHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        
        self.titleLabel = [[UILabel alloc]init];
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.textColor = [UIColor xm_jetblackColor];
        self.titleLabel.font = [UIFont xm_medium:22.0];
        [self addSubview:self.titleLabel];
        
        self.userView = [[NewsUserView alloc]init];
        [self addSubview:self.userView];
        
        self.detailView = [[NewsDetailView alloc] init];
        WEAKSELF
        self.detailView.heightBlock = ^(CGFloat webHeight) {
            weakSelf.detailHeight = webHeight;
            [weakSelf setNeedsLayout];
        };
        [self addSubview:self.detailView];
        
        self.sourceLabel = [[UILabel alloc] init];
        self.sourceLabel.textColor = [UIColor xm_somberColor];
        self.sourceLabel.font = [UIFont xm_font:14.0];
        [self addSubview:self.sourceLabel];
        
        self.authorLabel = [[UILabel alloc] init];
        self.authorLabel.textColor = [UIColor xm_somberColor];
        self.authorLabel.font = [UIFont xm_font:14.0];
        [self addSubview:self.authorLabel];
        
        self.statementLabel = [[XMLabel alloc] init];
        self.statementLabel.backgroundColor = [UIColor xm_colorFromRGB:0x5D646E alpha:0.04];
        self.statementLabel.numberOfLines = 0;
        self.statementLabel.textColor = [UIColor xm_somberColor];
        self.statementLabel.font = [UIFont xm_font:14.0];
        self.statementLabel.edgeInsets = UIEdgeInsetsMake(10, 20, 10, 20);
        [self addSubview:self.statementLabel];
        
        
        self.shareView = [[NewsShareView alloc] init];
        [self addSubview:self.shareView];
        
        self.lineView = [[UIView alloc] init];
        self.lineView.backgroundColor = [UIColor xm_splitlineColor];
        [self addSubview:self.lineView];
        
        self.allCommentLabel = [[UILabel alloc] init];
        self.allCommentLabel.text = @"全部评论";
        self.allCommentLabel.font = [UIFont xm_medium:17.0];
        [self addSubview:self.allCommentLabel];
        
    }
    return self;
}
- (void)setDetailModel:(AticleDetailModel *)detailModel{
    _detailModel = detailModel;
    self.titleLabel.text =_detailModel.Title;
    self.userView.detailModel = _detailModel;
    self.detailView.detailModel = _detailModel;
    
    self.sourceLabel.text = [NSString stringWithFormat:@"本文来源：%@",_detailModel.Souce];
    self.authorLabel.text = [NSString stringWithFormat:@"本文作者：%@",_detailModel.Author];
    self.statementLabel.attributedText = [XMTool attributedWithString:_detailModel.Statement font:[UIFont xm_font:14.0] lineSpace:5.0];
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGSize titleSize = [self.detailModel.Title xm_sizeWithFont:[UIFont xm_medium:22.0] maxW:self.width - 40];
    self.titleLabel.x = 20;
    self.titleLabel.y = 0;
    self.titleLabel.size = titleSize;

    self.userView.frame    = CGRectMake(20, CGRectGetMaxY(self.titleLabel.frame) + 20, self.width - 40, 40);
    self.detailView.frame  = CGRectMake(0, CGRectGetMaxY(self.userView.frame) + 20, self.width, self.detailHeight);
    self.sourceLabel.frame = CGRectMake(20, CGRectGetMaxY(self.detailView.frame), self.width - 40, 15);
    self.authorLabel.frame = CGRectMake(20, CGRectGetMaxY(self.sourceLabel.frame) + 5, self.width - 40, 15);


    CGFloat statementHeight = [XMTool getSpaceLabelHeight:self.detailModel.Statement
                                                 withFont:[UIFont xm_font:14.0]
                                                withWidth:self.width - 40
                                                lineSpace:5.0];
    self.statementLabel.x = 0;
    self.statementLabel.y = CGRectGetMaxY(self.authorLabel.frame)+20;
    self.statementLabel.size = CGSizeMake(self.width, statementHeight +20);
    
    self.shareView.frame = CGRectMake(0, CGRectGetMaxY(self.statementLabel.frame)+20, self.width, 120);
    
    self.lineView.frame = CGRectMake(0, CGRectGetMaxY(self.shareView.frame) + 20, self.width, 10);
    self.allCommentLabel.frame = CGRectMake(15, CGRectGetMaxY(self.lineView.frame), 200, 50);
    self.heightBlock(CGRectGetMaxY(self.allCommentLabel.frame));
}
- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
}


@end
